# == Schema Information
# Schema version: 22
#
# Table name: documents
#
#  id              :integer       not null, primary key
#  attachable_id   :integer       
#  content_type    :string(255)   
#  filename        :string(255)   
#  size            :integer       
#  created_at      :date          
#  attachable_type :string(255)   
#

class Document < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
  has_attachment :storage => :file_system,
                 :content_type => ['application/pdf', 'application/msword', 'text/plain'],
                 :max_size => 10.megabytes
  validate :attachment_attributes_valid?
  has_many :reviews, :dependent => :destroy

  def attachment_attributes_valid?
    errors.add :size, "of your article must be less then 10 megabytes" unless attachment_options[:size].nil? || attachment_options[:size].include?(send(:size))
    errors.add_to_base "This is not a supported document type.  Please use .pdf, .doc, or .txt" unless attachment_options[:content_type].nil? || attachment_options[:content_type].include?(send(:content_type))
  end

  def pretty_content_type
    case content_type
      when 'application/pdf'
        'PDF'
      when 'application/msword'
        'DOC'
      when 'text/plain'
        'TEXT'
    end
  end
end


